package com.huatai.bpmn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.commom.emnu.StatusCode;
import com.huatai.bpmn.entity.YYBasicEntity;
import com.huatai.bpmn.mapper.BasicMapper;
import com.huatai.bpmn.entity.BasicEntity;
import com.huatai.bpmn.service.BasicDataService;
import com.huatai.bpmn.service.BasicLoadService;
import com.huatai.bpmn.service.BasicService;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.common.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  表单表头配置
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@Service
public class BasicServiceImpl extends ServiceImpl<BasicMapper, BasicEntity> implements BasicService {

	@Autowired
	private BasicLoadService basicLoadService;

	/**
	 * 根据表单标识获取表单表头
	 * @param place
	 * @return
	 */
	@Override
	public List<BasicEntity> getColum(String place) {
		List<BasicEntity> list = this.list(new LambdaQueryWrapper<>(BasicEntity.class).like(BasicEntity::getPlace,place).eq(BasicEntity::getPid,"0"));
		list.stream().sorted(Comparator.comparing(BasicEntity::getSort));
		list.stream().forEach(x->{
			//通过父id查询子数据，目前默认表头最多为2，如果可能存在多个，可以参照字典递归即可
			String parentId = x.getId();
			List<BasicEntity> childrenList = this.list(new LambdaQueryWrapper<>(BasicEntity.class).like(BasicEntity::getPlace, place).eq(BasicEntity::getPid, parentId));
			x.setChildren(childrenList);
		});
		return list;
	}

	/**
	 * 获取表单所有表头
	 * @param place
	 * @return
	 */
	@Override
	public List<BasicEntity> getColumList(String place) {
		List<BasicEntity> list = this.list(new LambdaQueryWrapper<>(BasicEntity.class).like(BasicEntity::getPlace, place));

		//用于保存需要移除的数据
		ArrayList<BasicEntity> basicEntityList = new ArrayList<>();
		//因为place为x,x,x类型，使用like查询可能存在某些字段不属于place，过滤掉表头place不相同的表头
		list.stream().forEach(x->{
			if (x.getPlace().contains(",")){
				String[] array = x.getPlace().split(",");
				boolean b = Arrays.stream(array).anyMatch(y -> y.equals(place));
				if (!b){
					basicEntityList.add(x);
				}
			}else{
				if (!x.getPlace().equals(place)){
					basicEntityList.add(x);
				}
			}
		});
		list.removeAll(basicEntityList);
		return list;
	}

	/**
	 * 获取外部参数表单数据
	 * @return
	 */
	@Override
	public Map<String, Object> getExternalParameterColum(String deployId) {
		HashMap<String, Object> map = new HashMap<>();
		List<BasicEntity> colum = this.getColum(StatusCode.FORM_EXTERNAL_PARAMETER.getCode());
		//排除该表头，方便前端渲染
		colum=colum.stream().filter(x->!x.getJsonKey().equals("deployId")).collect(Collectors.toList());
//		List<JSONObject> list = basicDataService.listByDeployId(deployId);
		map.put("colum",colum);
//		map.put("data",list);
		return map;
	}
	/**
	 * 查询表头
	 * @param str
	 * @return
	 */
	@Override
	public List<YYBasicEntity> getBasicList(String str) {
		return this.getBaseMapper().getBasicList(str);
	}

	@Override
	public boolean removeData(String id) {
		List<BasicEntity> list = this.list(new LambdaQueryWrapper<>(BasicEntity.class).eq(BasicEntity::getPid, id));
		if (list.size()>0){
		throw new ServiceException("存在子节点，无法删除");
		}
		BasicEntity basicEntity = this.getById(id);
		if (ObjectUtil.isEmpty(basicEntity)){
			throw new ServiceException("数据不存在");
		}
		boolean flag = this.removeById(id);
		//删除数据中存在的字段
		basicLoadService.removeByKey(basicEntity.getJsonKey());
		return false;
	}


}
